package cn.wjh.business.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

import cn.wjh.business.domain.Translate;
import cn.wjh.business.domain.vo.TranslateVo;
import cn.wjh.common.utils.DateUtils;
import cn.wjh.common.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import cn.wjh.business.mapper.TranslateHistoryMapper;
import cn.wjh.business.domain.TranslateHistory;
import cn.wjh.business.service.ITranslateHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 翻译记录Service业务层处理
 *
 * @author jiraWu
 * @date 2023-12-18
 */
@Service
public class TranslateHistoryServiceImpl extends ServiceImpl<TranslateHistoryMapper, TranslateHistory> implements ITranslateHistoryService {
    @Resource
    private TranslateHistoryMapper translateHistoryMapper;

    /**
     * 查询翻译记录
     *
     * @param translateHistoryId 翻译记录主键
     * @return 翻译记录
     */
    @Override
    public TranslateHistory selectTranslateHistoryByTranslateHistoryId(Long translateHistoryId) {
        return translateHistoryMapper.selectTranslateHistoryByTranslateHistoryId(translateHistoryId);
    }

    /**
     * 查询翻译记录列表
     *
     * @param translateHistory 翻译记录
     * @return 翻译记录
     */
    @Override
    public List<TranslateHistory> selectTranslateHistoryList(TranslateHistory translateHistory) {
        return translateHistoryMapper.selectTranslateHistoryList(translateHistory);
    }

    /**
     * 新增翻译记录
     *
     * @param translateHistory 翻译记录
     * @return 结果
     */
    @Override
    public int insertTranslateHistory(TranslateHistory translateHistory) {
        translateHistory.setCreateTime(DateUtils.getNowDate());
        return translateHistoryMapper.insertTranslateHistory(translateHistory);
    }

    /**
     * 修改翻译记录
     *
     * @param translateHistory 翻译记录
     * @return 结果
     */
    @Override
    public int updateTranslateHistory(TranslateHistory translateHistory) {
        return translateHistoryMapper.updateTranslateHistory(translateHistory);
    }

    /**
     * 批量删除翻译记录
     *
     * @param translateHistoryIds 需要删除的翻译记录主键
     * @return 结果
     */
    @Override
    public int deleteTranslateHistoryByTranslateHistoryIds(Long[] translateHistoryIds) {
        return translateHistoryMapper.deleteTranslateHistoryByTranslateHistoryIds(translateHistoryIds);
    }

    /**
     * 删除翻译记录信息
     *
     * @param translateHistoryId 翻译记录主键
     * @return 结果
     */
    @Override
    public int deleteTranslateHistoryByTranslateHistoryId(Long translateHistoryId) {
        return translateHistoryMapper.deleteTranslateHistoryByTranslateHistoryId(translateHistoryId);
    }

    /**
     * 翻译历史是否唯一
     * 唯一 true
     */
    @Override
    public boolean uniqueTranslationHistory(TranslateVo translate) {
        TranslateHistory translateHistory = getTranslationHistoryByTranslate(SecurityUtils.getUserId(), translate);
        return null == translateHistory;
    }

    /**
     * 获取一条翻译历史
     */
    @Override
    public TranslateHistory getTranslationHistoryByTranslate(Long userId, TranslateVo translate) {
        LambdaQueryWrapper<TranslateHistory> selectOneWrapper = new QueryWrapper<TranslateHistory>().lambda()
                .eq(userId != null, TranslateHistory::getUserId, userId)//用户Id
                .eq(TranslateHistory::getProductId, translate.getProductId())//产品Id
                .eq(TranslateHistory::getQ, translate.getQ())//待翻译的文本
                .eq(TranslateHistory::getFromLanguageCode, translate.getFrom())//源语言
                .eq(TranslateHistory::getToLanguageCode, String.join(",", translate.getToList()))//目标语言
                .orderByDesc(TranslateHistory::getCreateTime)//创建时间
                .last("limit 1");//一条
        return translateHistoryMapper.selectOne(selectOneWrapper);
    }

    @Override
    public TranslateHistory getTranslationHistoryByTranslate(Long userId, Translate translate) {
        TranslateVo translateVo = new TranslateVo();
        translateVo.setProductId(translate.getProductId());
        translateVo.setQ(translate.getTranslateVal());
        translateVo.setFrom(translate.getLanguageCode());
        List<String> toList = translate.getChildren().stream().map(Translate::getLanguageCode).collect(Collectors.toList());
        translateVo.setToList(new HashSet<>(toList));
        return this.getTranslationHistoryByTranslate(userId, translateVo);
    }
}
